package cn.easier.activity.console.repository;

import cn.easier.activity.web.entity.QuestionnaireMessage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * 模板信息 Repository
 */
@Repository
public interface QuestionnaireMessageRepository extends JpaRepository<QuestionnaireMessage, Integer>, JpaSpecificationExecutor<QuestionnaireMessage> {

    List<QuestionnaireMessage> findByIsDelAndActivityInfoId(int isDel, int activityInfoId);

    @Query("select distinct (a.activityInfoId) from QuestionnaireMessage a where a.type = :type and a.isDel=0 ")
    List<Integer> findActivityId(@Param("type") String type);

    List<QuestionnaireMessage> findByIsShowAndIsDel(int isShow, int isDel);

    List<QuestionnaireMessage> findByActivityInfoIdAndIsDelAndIsShowAndYesterdayShow(int activityInfoId, int isDel, int isShow, int yesterdayShow);

    @Query(value = "select a.* from questionnaire_message a where ((:activityInfoId is null or :activityInfoId = '') or a.`activity_info_id` = :activityInfoId) and a.`is_Del` =0 and a.type = :type order by a.id desc LIMIT :pageCount , :pageSize ", nativeQuery = true)
    List<QuestionnaireMessage> findAllBy(@Param("activityInfoId") Integer activityInfoId, @Param("type") String type, @Param("pageCount") int pageCount, @Param("pageSize") int pageSize);

    @Query(value = "select count(a.id) from questionnaire_message a where ((:activityInfoId is null or :activityInfoId = '') or a.`activity_info_id` = :activityInfoId) " +
            " and a.`is_Del` =0 and a.type = :type order by a.id desc ", nativeQuery = true)
    long findAllByCount(@Param("activityInfoId") Integer activityInfoId,@Param("type") String type);

    @Query(value = "select a.* from questionnaire_message a where ((:activityInfoId is null or :activityInfoId = '') or a.`activity_info_id` = :activityInfoId) and a.activity_info_id in :idList and a.`is_Del` =0 and a.type = :type order by a.id desc LIMIT :pageCount , :pageSize ", nativeQuery = true)
    List<QuestionnaireMessage> findAllByByUser(@Param("activityInfoId") Integer activityInfoId, @Param("type") String type, @Param("idList") List<Integer> idList ,@Param("pageCount") int pageCount, @Param("pageSize") int pageSize);

    @Query(value = "select count(a.id) from questionnaire_message a where ((:activityInfoId is null or :activityInfoId = '') or a.`activity_info_id` = :activityInfoId) " +
            " and a.`is_Del` =0 and a.type = :type and a.activity_info_id in :idList order by a.id desc ", nativeQuery = true)
    long findAllByCountAndUser(@Param("activityInfoId") Integer activityInfoId,@Param("type") String type ,@Param("idList") List<Integer> idList);


    @Query("select distinct(a.activityInfoId) from QuestionnaireMessage a where a.isDel=0 and a.type= :type ")
    List<Integer> findAllActivityName(@Param("type") String type);

    QuestionnaireMessage findByIdAndIsDel(Long id, int isDel);

    List<QuestionnaireMessage> findByType( String type);
}
